package com.topisv.tms.web.work.inscope;

import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import com.topisv.tms.entity.AreaGroup;
import com.topisv.tms.entity.Customers;
import com.topisv.tms.entity.LogisticsCompany;
import com.topisv.tms.entity.work.inscope.InScope;
import com.topisv.tms.service.AreaGroupService;
import com.topisv.tms.service.CustomersService;
import com.topisv.tms.service.LogisticsCompanyService;
import com.topisv.tms.service.account.ShiroDbRealm.ShiroUser;
import com.topisv.tms.service.work.inscope.InScopeService;

/**
 * 超区件—control
 * @author ch
 *
 */
@Controller
@RequestMapping(value = "/v2/inScope")
public class InScopeController {

	private static final Logger logger = LoggerFactory.getLogger(InScopeController.class);
	
	@Autowired
	private LogisticsCompanyService logisticsCompanyService;
	@Autowired
	private InScopeService inscopeService;
	@Autowired
	private AreaGroupService areaGroupService;
	@Autowired
	private CustomersService customersService;
	/**
	 * 导向超区件管理页面
	 * @param model
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "toInScopeMgrPage")
	public String toInscopeMgrPage(Model model, ServletRequest request) {
		List<LogisticsCompany> cmpList =this.logisticsCompanyService.findAll();
		model.addAttribute("cmpList", cmpList);
		return "work/inscope/inScopeMgr";
	}
	

	/**
	 * 导向超区件设置页面
	 * @param model
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "toInScopeSettingPage/{companyCode}")
	public String toInscopeSettingPage(@PathVariable("companyCode")String companyCode,Model model, ServletRequest request) {
		try {
			ShiroUser curUser = (ShiroUser)SecurityUtils.getSubject().getPrincipal();
			List<AreaGroup> warehouseList  = areaGroupService.findByAreaGroups(curUser.areagroups);
			//超区设置
			Map<String, String> map = new HashMap<String, String>();
			map.put("companyCode", companyCode);
			List<InScope> settingList=this.inscopeService.loadInScope(map);
			model.addAttribute("settingList", settingList);
			model.addAttribute("companyCode", companyCode);
			model.addAttribute("warehouseList", warehouseList);
		} catch (Exception e) {
			
			logger.error("error",e);
		}
		
		return "work/inscope/inScopeSetting";
	}
	
	/**
	 * 添加超区
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "ajaxAddInScope")
	public void ajaxAddInscope(ServletRequest request,HttpServletResponse response) {
		PrintWriter out=null;
		JSONObject json=null;
		String msg=null;
		
		try {	
			out = response.getWriter();
			json = new JSONObject();
			
			ShiroUser curUser = (ShiroUser) SecurityUtils.getSubject().getPrincipal();
			
			String companyCode=request.getParameter("companyCode");
			String zoneName=request.getParameter("zoneName");
			String zoneFullName=request.getParameter("zoneFullName");
			String zoneType=request.getParameter("zoneType");
			String warehouseCode=request.getParameter("warehouseCode");
			msg=this.inscopeService.addInScope(companyCode,zoneName,zoneFullName,zoneType,warehouseCode,curUser.jobuuid);
	
			Map<String, String> map = new HashMap<String, String>();
			map.put("companyCode", companyCode);
			map.put("warehouseCode", warehouseCode);
			List<InScope> settingList=this.inscopeService.loadInScope(map);
			
		    json.put("msg", msg);
		    json.put("settingList", settingList);
		    
			out.print(json);
			
		} catch (Exception e) {
			
			json.put("msg", "error");
			out.print(json);
		}
	}
	
	/**
	 * 删除超区
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "ajaxDeleteInScope")
	public void ajaxDeleteInscope(ServletRequest request,HttpServletResponse response) {
		PrintWriter out=null;
		JSONObject json=null;
		try {	
			out = response.getWriter();
			json = new JSONObject();
			
			Long InscopeId=Long.valueOf(request.getParameter("inScopeId"));
			this.inscopeService.deleteInScope(InscopeId);
			String warehouseCode=request.getParameter("warehouseCode");
			String companyCode=request.getParameter("companyCode");
			Map<String, String> map = new HashMap<String, String>();
			map.put("companyCode", companyCode);
			map.put("warehouseCode", warehouseCode);
			List<InScope> settingList=this.inscopeService.loadInScope(map);
		    json.put("settingList", settingList);
		    
			out.print(json);
			
		} catch (Exception e) {
			
			json.put("msg", "error");
			out.print(json);
		}
	}
	
	/**
	 * 查询超区
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "ajaxSelectInScope")
	public void ajaxSelectInScope(ServletRequest request,HttpServletResponse response) {
		PrintWriter out=null;
		JSONObject json=null;
		try {	
			out = response.getWriter();
			json = new JSONObject();
			
			String companyCode=request.getParameter("companyCode");
			String warehouseCode=request.getParameter("warehouseCode");
			Map<String, String> map = new HashMap<String, String>();
			map.put("companyCode", companyCode);
			map.put("warehouseCode", warehouseCode);
			List<InScope> settingList=this.inscopeService.loadInScope(map);
		    json.put("settingList", settingList);
		    
			out.print(json);
			
		} catch (Exception e) {
			logger.info("", e);
		}
	}
	
	
	/**
	 * 查询配置适用商户
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "ajaxInScopeConfigCustomers")
	public void ajaxInScopeConfigCustomers(ServletRequest request,HttpServletResponse response) {
		PrintWriter out=null;
		JSONObject json=null;
		String status=null;
		
		try {	
			out = response.getWriter();
			json = new JSONObject();
			
			String inScopeId=request.getParameter("inScopeId");
			if(StringUtils.isEmpty(inScopeId)){
				status = "error";
			}else {
				InScope inScope=this.inscopeService.loadInScopeOne(Long.valueOf(inScopeId));
				List<Customers> allConfigCustomers = this.customersService.findAll();
				
				json.put("allConfigCustomers", allConfigCustomers);
				json.put("assignCustomers", inScope.getCustomerConfigList());
				status = "success";
			}
		    json.put("status", status);
			out.print(json);
			
		} catch (Exception e) {
			
			json.put("msg", "error");
			out.print(json);
		}
	}
	/**
	 * 保存配置适用商户
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "ajaxSaveInScopeConfigCustomers")
	public void ajaxSaveInScopeConfigCustomers(ServletRequest request,HttpServletResponse response) {
		PrintWriter out=null;
		JSONObject json=null;
		String status=null;
		
		try {	
			out = response.getWriter();
			json = new JSONObject();
			
			String inScopeId=request.getParameter("inScopeId");
			String customerCodes=request.getParameter("customerCodes");
			if(StringUtils.isEmpty(inScopeId)){
				status = "error";
			}else {
				this.inscopeService.saveInScopeConfigCustomers(inScopeId, customerCodes);
				status = "success";
			}
		    json.put("status", status);
			out.print(json);
		} catch (Exception e) {
			json.put("msg", "error");
			out.print(json);
		}
	}
	
	
}
